package com.ds.core.support.qiniu;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.qiniu.util.Auth;

public class Downloader {

	/**
	 * 公有空间返回文件URL
	 * @param fileName
	 * @param domainOfBucket
	 * @return
	 */
	public static String publicFile(String fileName,String domainOfBucket) {
		//String fileName = "FuWyOfil3ZgwGnBANuHJ265ubed2";
		//String domainOfBucket = "http://oyl97txx0.bkt.clouddn.com";
		String encodedFileName=null;
		try {
			encodedFileName = URLEncoder.encode(fileName, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String finalUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
		System.out.println(finalUrl);
		return finalUrl;
	}
	
	/**
	 * 私有空间返回文件URL
	 * @param auth
	 * @param fileName
	 * @param domainOfBucket
	 * @param expireInSeconds
	 * @return
	 */
	public static String privateFile(Auth auth,String fileName,String domainOfBucket,long expireInSeconds) {
		//String fileName = "七牛/云存储/qiniu.jpg";
		//String domainOfBucket = "http://devtools.qiniu.com";
		String encodedFileName=null;
		try {
			encodedFileName = URLEncoder.encode(fileName, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
		String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
		System.out.println(finalUrl);
		return finalUrl;
	}	
	
	public static void main(String[] args) {
		//publicFile("IMG_0539.MP4","http://oyl97txx0.bkt.clouddn.com");
		privateFile(CredentialsManager.getAuth(),"IMG_0539.MP4","http://oyl97txx0.bkt.clouddn.com",3600);

	}
}
